package assist.client.gwt.view.ui;


import assist.client.gwt.service.StatsProvider;
import assist.domain.model.Stats;
import assist.domain.model.StatsQuery;
import assist.domain.model.Stats.Row;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;

public class StatsTable extends CellTable<Stats.Row>
{    
    public void setStatsProvider(StatsProvider statsProvider)
    {
        this.setStatsQuery(statsProvider.getStatsQuery());
        statsProvider.addDataDisplay(this);
    }

    public void setStatsQuery(StatsQuery statsQuery)
    {
        this.clearColumns();
        this.addColumn(new RowQueryColumn(), "Player(s)");
        for (StatsQuery.Column column : statsQuery.getColumns())
        {
            this.addColumn(new StatsColumn(column), 
                    new StatsHeader(column));
        }
         
    }

    private void clearColumns()
    {
        while (this.getColumnCount() > 0)
        {
            this.removeColumn(0);
        }
    }
    
    public class StatsHeader extends Header<StatsQuery.Column>
    {
        
        private StatsQuery.Column column;
        public StatsHeader(StatsQuery.Column column)
        {
            super(new StatsHeaderCell());
            this.column = column;
        }

        @Override
        public StatsQuery.Column getValue()
        {
            // TODO Auto-generated method stub
            return this.column;
        }

    }    
    
    public static class RowQueryColumn extends Column<Row, String>
    {       
        public RowQueryColumn()
        {
            super(new TextCell());
        }

        @Override
        public String getValue(Row row)
        {            
            return row.getRowQuery().getTitle();
        }
    }  

    public static class StatsColumn extends Column<Row, String>
    {
        private StatsQuery.Column column;
        public StatsColumn(StatsQuery.Column column)
        {
            super(new TextCell());
            this.column = column;
        }

        @Override
        public String getValue(Row row)
        {
            
            //TODO: Stats.Row will be a map of values to Filters Sets.
            //TODO: Will need to add nested headerTypes.
            return row.getFormatted(column);
        }
    }    
    
   
    public static class StatsHeaderCell extends AbstractCell<StatsQuery.Column>
    {
        @Override
        public void render(Context context, StatsQuery.Column filter, SafeHtmlBuilder sb) {
          /*
           * Always do a null check on the value. Cell widgets can pass null to
           * cells if the underlying data contains a null, or if the data arrives
           * out of order.
           */
          if (filter == null) {
            return;
          }

          // If the value comes from the user, we escape it to avoid XSS attacks.
          SafeHtml safeValue = SafeHtmlUtils.fromString(filter.toString());
       
          sb.appendHtmlConstant("<div>");
          sb.append(safeValue);
          sb.appendHtmlConstant("</div>");
        }
    }    
}
